
function EnvCreateController($scope, dataFactory, alertService) {

    $scope.title = "创建环境信息";
    $scope.create_info = true;

    $scope.env_create = function() {

        var post_data = {
            'Name' : $scope.env_name,
            'Description': $scope.env_desc
        }

        dataFactory.create_env(post_data).success(function (response) {

            if (response.ChangeState == 'success') {

                $scope.create_info = false;

                alertService.add('success', '<h4 class="text-center alert alert-success">已成功创建! 3秒后跳转!</h4>')

                setTimeout(function() {
                    alertService.closeAlert(0);
                    location.href='#/envs';
                }, 3000);
            }
        });
    }
}


function EnvController($scope, dataFactory) {

    $scope.title = "环境信息列表";

    dataFactory.get_envs(1, 5).success(function(data) {
        $scope.totalItems = data.count;
//        console.log($scope.bigTotalItems);
    });

    $scope.$watch("currentPage", function() {
        var start_point = ($scope.currentPage -1) * $scope.itemsPerPage + 1;
        var response = dataFactory.get_envs( start_point, $scope.itemsPerPage);

        response.success(function(data) {
            $scope.env_list = data.envs;
        });
    })
}

function EnvDelController($scope, $routeParams, dataFactory, alertService) {

    $scope.title = "删除环境";
    $scope.del_info = true;

    var data_source = dataFactory.get_env_by_id($routeParams.id)
    data_source.success(function(data) {
        $scope.env = data;
    });

    $scope.del_comfirm = function () {
        dataFactory.delete_env($scope.env).success(function (response) {
            if (response.ChangeState == 'success') {
//                $scope.env_del_alert = true;
                $scope.del_info = false;

                alertService.add('success', '<h4 class="text-center alert alert-success">已成功删除! 3秒后跳转!</h4>')

                setTimeout(function() {
                    alertService.closeAlert(0);
                    location.href='#/envs';
                }, 3000);
            }
        });
    }
}

function EnvEditController($scope, $routeParams, alertService, dataFactory) {

    $scope.title = "编辑环境信息";
    $scope.edit_env_info = true;
//    alertService.add('warning', '这是一个警告消息!')

    var init = function() {
        dataFactory.get_env_by_id($routeParams.id).success(function(response) {
            $scope.env = response;
        });
    }

    init();

    $scope.reset = function () {
        init();
    }

    $scope.comfirm = function() {

        dataFactory.update_env($scope.env).success(function(response) {

            if (response.ChangeState=='success') {

                $scope.edit_env_info = false;
                $scope.env_update_alert = true;

                alertService.add('success', '<h4 class="text-center alert alert-success">已成功更新! 3秒后跳转!</h4><a href="#/envs">点击直接跳转!</a>')
                setTimeout(function() {
                    alertService.closeAlert(0);
                    location.href='#/envs'
                }, 3000);
            }
        });
    }
}

